<?php if (!defined("_ECRIRE_INC_VERSION")) return; if (class_exists('AlohacartPaymentInterkassa')) return;
/**
 * Aloha Cart
 *
 * @author     Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 * @license    GNU/GPL
 * @package    plugins
 * @subpackage alohacart
 * @copyright  (c) Aleksey Yaroshchuk 2011-2012
 * @link       http://www.spip-contrib.net/AlohaCart
 */

/** 
 *  This class provides interkassa payment logic
 *
 *  @author Aleksey Yaroshchuk (aleksey.yaroshchuk@gmail.com)
 */
include_spip('functions/pdk_common');
pdk_load_class('AlohacartExtension', 'classes'); 
class AlohacartPaymentInterkassa extends AlohacartExtension {
    public $fields = array('shop_id', 'secret_key', 'id_currency','id_order_statut_after_payment');
    
    public function __construct() {
        parent::__construct();
        $this->configExtension();
    }
    
    private function configExtension(){
        $this->type = 'payment';
        $this->code = 'interkassa';
        $this->isPayable = true;
        
        // Define validation rules:         
         $this->validation->set_rules( 
            array(
               array(
                     'field'   => 'shop_id',
                     'label'   => _T('alohacart_payment_interkassa:shop_id'),
                     'rules'   => 'required'
                  ),
               array(
                     'field'   => 'secret_key',
                     'label'   => _T('alohacart_payment_interkassa:secret_key'),
                     'rules'   => 'required'
                  ),
            )
         );
    }
    
    public function GetData($options=array()) {
        // Try to get id_order from $options array or request
        if ($options['id_order']) $id_order = $options['id_order'];
        if (!$id_order && _request('id_order')) $id_order = _request('id_order');
        if (!$id_order) return '';
        
        $data = $this->GetMetaData();
        $order_mdl = pdk_load_class('AlohacartOrderModel', 'classes');
        $currency_mdl = pdk_load_class('CurrencyModel','classes/');
        
        $order = $order_mdl->select_by_id($id_order);
        if (!$order) return;
        $data['id_order'] = $id_order;
        $data['order_total_price'] = $order['total_price'];
        $data['order_name'] = $order['name'];
        
        // If there is USD currency on this site, we will transfer main currency rate (against USD - Interkassa's style)
        // Actually, for our style this is USD rate against main currency
        $currency_usd = $currency_mdl->SelectByCode('USD');
        $data['currency_usd_rate'] = $currency_usd['rate'];
        
        $data['payment_description'] = _T('alohacart_payment_platilka:payment_description', array('nom_site_spip'=>extraire_multi($GLOBALS['meta']['nom_site'])));
        
        return recuperer_fond('inc/payment/alohacart_interkassa', $data);
    }
    
    /**
     *  Checks hash of status data
     *  @param array $status_data Status data
     *  @return bool
     */
    public function CheckHash($status_data) {
        $meta_data = $this->GetMetaData();
        $secret_key = $meta_data['secret_key'];
        
        $sing_hash_str = $status_data['ik_shop_id'].':'.
            $status_data['ik_payment_amount'].':'.
			$status_data['ik_payment_id'].':'.
			$status_data['ik_paysystem_alias'].':'.
			$status_data['ik_baggage_fields'].':'.
			$status_data['ik_payment_state'].':'.
			$status_data['ik_trans_id'].':'.
			$status_data['ik_currency_exch'].':'.
			$status_data['ik_fees_payer'].':'.
			$secret_key;
            
        $sign_hash = strtoupper(md5($sing_hash_str));
        
        if($status_data['ik_sign_hash'] === $sign_hash) 
            return true;
        else
            return false;
    }
    
    public function CreatePaymentData($data) {
        $string = '';
        $string .= isset($data['ik_shop_id'])&&$data['ik_shop_id']?_T('alohacart_payment_interkassa:shop_id').':'.$data['ik_shop_id']."\n":'';
        $string .= isset($data['ik_payment_id'])&&$data['ik_payment_id']?_T('alohacart_payment_interkassa:ik_payment_id').':'.$data['ik_payment_id']."\n":'';
        $string .= isset($data['ik_payment_desc'])&&$data['ik_payment_desc']?_T('alohacart_payment_interkassa:ik_payment_desc').':'.$data['ik_payment_desc']."\n":''; 
        $string .= isset($data['ik_paysystem_alias'])&&$data['ik_paysystem_alias']?_T('alohacart_payment_interkassa:ik_paysystem_alias').':'.$data['ik_paysystem_alias']."\n":'';
        $string .= isset($data['ik_baggage_fields'])&&$data['ik_baggage_fields']?_T('alohacart_payment_interkassa:ik_baggage_fields').':'.$data['ik_baggage_fields']."\n":'';
        $string .= isset($data['ik_payment_timestamp'])&&$data['ik_payment_timestamp']?_T('alohacart_payment_interkassa:ik_payment_timestamp').':'.$data['ik_payment_timestamp']."\n":'';
        $string .= isset($data['ik_payment_state'])&&$data['ik_payment_state']?_T('alohacart_payment_interkassa:ik_payment_state').':'.$data['ik_payment_state']."\n":'';
        $string .= isset($data['ik_trans_id'])&&$data['ik_trans_id']?_T('alohacart_payment_interkassa:ik_trans_id').':'.$data['ik_trans_id']."\n":'';
        $string .= isset($data['ik_currency_exch'])&&$data['ik_currency_exch']?_T('alohacart_payment_interkassa:ik_currency_exch').':'.$data['ik_currency_exch']."\n":'';
        $string .= isset($data['ik_fees_payer'])&&$data['ik_fees_payer']?_T('alohacart_payment_interkassa:ik_fees_payer').':'.$data['ik_fees_payer']."\n":'';
        $string .= isset($data['ik_sign_hash'])&&$data['ik_sign_hash']?_T('alohacart_payment_interkassa:ik_sign_hash').':'.$data['ik_sign_hash']."\n":'';
        return $string;
    }
}
?>